home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATATYPE.SWG / 0021_Card Shuffling.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  5KB  |  147 lines

  1. program CardGame;
  2. { Author: John Howard  jh
  3.   Date: 08-SEP-94
  4.   Version: 1.0
  5.   Demonstrate two methods to shuffle cards.  Swap method is fast!
  6.   Demonstrate how to evaluate Bridge hands:
  7.     Each hand of 13 cards are arranged by suits and rank within suit (aces are
  8.     high).  The hand is then evaluated using the following standard bridge
  9.     values.
  10.  
  11.    Aces count 4
  12.    Kings count 3
  13.    Queens count 2
  14.    Jacks count 1
  15.    Voids (no cards in a suit) count 3
  16.    Singletons (one card in a suit) count 2
  17.    Doubletons (two cards in a suit) count 1
  18.    Long suits with more than five cards in the suit
  19.       count 1 for each card over five in number
  20.  
  21.    Example: 2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH = 16 points
  22.    because there are 2 aces, 1 king, 1 queen, 1 jack, 1 singleton.
  23. }
  24. {$DEFINE BridgeHand}
  25. const
  26. {$IFDEF BridgeHand}
  27.    points : integer = 0;
  28. {$ENDIF}
  29.    maxdeck = 52;
  30.    maxsuit = 13;
  31.    sentinel = 0;
  32.    suits = 4;
  33.  
  34. type
  35.    card = byte;
  36.    suit = array[1..maxsuit] of card;    { ace rank is 1, king rank is 13 }
  37.  
  38. var
  39.    hearts, diamonds : suit;       {red}
  40.    clubs, spades : suit;          {black}
  41.    deck : array[1..maxdeck] of card;
  42.    i, j, k : integer;             { indices }
  43.    count : integer;               { count of used cards }
  44.  
  45. procedure swap(a,b : integer);
  46. var temp : integer;
  47. begin
  48.   temp := deck[a];
  49.   deck[a] := deck[b];
  50.   deck[b] := temp;
  51. end;
  52.  
  53. BEGIN  {main}
  54.   writeln('method one -  random swap.  Absolutely the fastest!');
  55.   randomize;
  56.   for i := 1 to maxdeck do        {initialize deck}
  57.     deck[i] := i;                 { Card number MODULO 13 will help to rank }
  58.  
  59.   for i := 1 to maxdeck do        {index card deck}
  60.     begin
  61.       j := random(maxdeck) +1;    { range is 1..52 }
  62.       SWAP(i, j);                 { shuffle two cards }
  63.     end;
  64.  
  65.   for i := 1 to maxdeck do        {index card deck}
  66.     write(deck[i], ' ');          { display card order }
  67.   writeln;
  68.  
  69.   writeln('method two -  rank each card.  Theoretically may take forever!');
  70.   randomize;
  71.   for i := 1 to maxdeck do        {initialize deck}
  72.     deck[i] := sentinel;          { Zero is our sentinel }
  73.  
  74.   for i := 1 to maxsuit do        {initialize suits with their card numbers}
  75.     begin
  76.       hearts[i]   := i;              { range is  1..13 }
  77.       diamonds[i] := i + maxsuit;    { range is 14..26 }
  78.       clubs[i]    := i + 2*maxsuit;  { range is 27..39 }
  79.       spades[i]   := i + 3*maxsuit;  { range is 40..52 }
  80.     end;
  81.  
  82.   count := maxdeck;
  83. {$IFDEF BridgeHand}
  84.   count := maxsuit;
  85. {$ENDIF}
  86.   repeat
  87.   i := random(maxdeck) +1;        { range is 1..52 }
  88.   if deck[i] = sentinel then
  89.     begin
  90.       j := random(maxsuit) +1;    { range is 1..13 }
  91.       k := random(suits) +1;      { range is 1..4 }
  92.       case k of
  93.         1 :
  94.               if hearts[j] <> sentinel then
  95.                 begin
  96.                   hearts[j] := sentinel;
  97.                   deck[i] := j;
  98.                   dec(count);
  99.                 end;
  100.         2 :
  101.               if diamonds[j] <> sentinel then
  102.                 begin
  103.                   diamonds[j] := sentinel;
  104.                   deck[i] := j+ maxsuit;
  105.                   dec(count);
  106.                 end;
  107.         3 :
  108.               if clubs[j] <> sentinel then
  109.                 begin
  110.                   clubs[j] := sentinel;
  111.                   deck[i] := j+ 2*maxsuit;
  112.                   dec(count);
  113.                 end;
  114.         4 :
  115.               if spades[j] <> sentinel then
  116.                 begin
  117.                   spades[j] := sentinel;
  118.                   deck[i] := j+ 3*maxsuit;
  119.                   dec(count);
  120.                 end;
  121.       end; {case}
  122.     end;
  123.   until (count = 0);
  124.  
  125.   for i := 1 to maxdeck do        {index card deck}
  126.     write(deck[i], ' ');          { display card order }
  127.   writeln;
  128.  
  129. {$IFDEF BridgeHand}
  130. { Only block for Hearts is shown.  Code is similar for each suit! }
  131.   count := 0;
  132.   if hearts[1]  = sentinel then inc(points, 4);      { ace }
  133.   if hearts[13] = sentinel then inc(points, 3);      { king }
  134.   if hearts[12] = sentinel then inc(points, 2);      { queen }
  135.   if hearts[11] = sentinel then inc(points, 1);      { jack }
  136.   for i := 1 to maxsuit do
  137.      if hearts[i] = sentinel then inc(count);
  138.   case count of
  139.      0: inc(points, 3);                              { void }
  140.      1: inc(points, 2);                              { singleton }
  141.      2: inc(points, 1);                              { doubleton }
  142.      6..13: inc(points, count-5);                    { long suit }
  143.   end; {case}
  144.   writeln('Points for Hearts = ', points);
  145. {$ENDIF}
  146. END. {program}
  147.